package com.kakarote.crm9.erp.admin.controller;

import cn.hutool.core.util.IdUtil;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.upload.UploadFile;
import com.kakarote.crm9.erp.admin.entity.AdminFile;
import com.kakarote.crm9.erp.admin.service.AdminFileService;
import com.kakarote.crm9.utils.BaseUtil;
import com.kakarote.crm9.utils.R;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import org.apache.commons.io.FileUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

public class AdminFileController extends Controller {
    @Inject
    private AdminFileService adminFileService;

    public final static Prop prop = PropKit.use("config/crm9-config.txt");

    public void index(){
        renderJson(R.ok());
    }

    /**
     * @author zhangzhiwei
     * 上传附件
     *
     */
    public void upload() throws IOException {
        String prefix=BaseUtil.getDate();
        HttpServletRequest request = getRequest();
        String uploadIp = BaseUtil.isWindows() ? BaseUtil.getIpAddress(request) : prop.get("upload-ip");
        String uploadUri = prop.get("upload-uri");
        request.setCharacterEncoding("UTF-8");

        UploadFile file = this.getFile("file",prefix,10000000,"UTF-8");
        // 文件重命名,采用日期分类
//        String newFileNameSuffix = cn.hutool.core.io.FileUtil.extName(file.getFileName());
      //  String newFileName = IdUtil.simpleUUID() + "." + newFileNameSuffix;//随机字符串生成新文件名
        String newFileDir = BaseUtil.isWindows() ? file.getUploadPath() + "/" + file.getFileName() :  uploadIp + "/static/crm1/" + prefix + "/" + file.getFileName() ;
        File fileNew=new File(newFileDir);
//        File fileOld = file.getFile();
////        FileUtils.moveFile(fileOld,fileNew);
//        fileOld.renameTo(fileNew);
//        Log.getLog(getClass()).info("修改文件名：" + fileOld.renameTo(fileNew));
        String newPath = BaseUtil.isWindows() ? fileNew.getPath() : uploadUri + prefix + "/" + file.getFileName();
        renderJson(adminFileService.upload(file,getPara("batchId"),getPara("type"),newFileDir,file.getFileName(),newPath,request));
    }

    /**
     * @author zhangzhiwei
     * 通过批次ID查询
     */
    public void queryByBatchId(){
        renderJson(R.ok().put("data", adminFileService.queryByBatchId(getPara("batchId"))));
    }

    /**
     * @author zhangzhiwei
     * 通过ID查询
     */
    public void queryById(){
        renderJson(adminFileService.queryById(getPara("id")));
    }

    /**
     * @author zhangzhiwei
     * 通过ID删除
     */
    public void removeById(){
        renderJson(adminFileService.removeById(getPara("id")));
    }

    /**
     * @author zhangzhiwei
     * 通过批次ID删除
     */
    public void removeByBatchId(){
        adminFileService.removeByBatchId(getPara("batchId"));
        renderJson(R.ok());
    }

    /**
     * @author zhangzhiwei
     * 重命名文件
     */
    public void renameFileById(){
        AdminFile file=new AdminFile();
        file.setFileId(getInt("fileId"));
        file.setName(getPara("name"));
        renderJson(adminFileService.renameFileById(file)?R.ok():R.error());
    }

    /**
     * 下载文件
     */
    public void downFile(@Para("fileId") String fileId) {
        fileId="/file/downFile?fileId="+fileId;
        AdminFile adminFile = AdminFile.dao.findFirst("SELECT * FROM `72crm_admin_file` where file_path=? LIMIT 0,1",fileId);
        if (adminFile != null) {
            renderFile(new File(adminFile.getPath()));
            return;
        }
        renderNull();
    }
}
